class WidgetManager:
    """Widget manager for loading and manage widgets"""
    
    def __init__(self):
        self.ModuleList = {}
        
    def __getitem__(self, Index):
        """Shortcut for accessing widgets"""
        return self.ModuleList[Index]
    
    def LoadWidgets(self):
        """Import widgets form file"""
        import sys, os
        
        try:
            import os
            import dircache
            import blockarticle
            
            WidgetPath = os.path.split(blockarticle.__file__)[0] 
            DirList = dircache.listdir(WidgetPath + '/widgets')
    
            for FileName in DirList:
                #exec 'import %s' % Module
                Name, Suffix = FileName.split('.')
                if Name != '__init__' and Suffix == 'py':
                    #print 'blockarticle.widgets.' + Name
                    BlockArticle = __import__('blockarticle.widgets.' + Name)
                    Widgets = getattr(BlockArticle, 'widgets')
                    self.ModuleList[Name] = getattr(Widgets, Name)
        except: 
            raise
        
    def GetWidget(self, TypeName):
        """Get a widget by its type name
        
        @param TypeName: Type name of widget, in "module.type" form 
        """
        Module, Type = TypeName.split('.')
        
        return getattr(self.ModuleList[Module], Type)